package com.example.lenovo.weiyi;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.com.ruanmeng.demon.JianCeList;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.BaseLoadMoreHeaderAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 定制服务
 */
public class DZServiceActivity extends BaseActivity {
    @BindView(R.id.rlist_service)
    RecyclerView rlistService;
    @BindView(R.id.refresh_service)
    SwipeRefreshLayout mRefresh;
    private View fview;
    private LinearLayoutManager linearLayoutManager;
    private JianCeList model;
    private LinearLayout li_null;
    int page = 1;
    ArrayList<JianCeList.FooterBean> Temp_listfw = new ArrayList<>();//后面追加服务列表
    private JianceListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.service_fragment_layout);
        ButterKnife.bind(this);
        ChangLayTitle("定制服务");
        LayBack();
        initView();
        getData();
    }

    private void initView() {
        //空
        li_null = (LinearLayout) findViewById(R.id.li_service_null);
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });
        linearLayoutManager = new LinearLayoutManager(DZServiceActivity.this);
        rlistService.setLayoutManager(linearLayoutManager);
        mAdapter = new JianceListAdapter(DZServiceActivity.this, rlistService, Temp_listfw, R.layout.item_dzservice);
        rlistService.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(DZServiceActivity.this, ServiceXQActivity.class);
                intent.putExtra("aid", Temp_listfw.get(position + 1).getOtoRecordId());
                startActivity(intent);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
//                page++;
//                getData();
            }
        });

    }

    public void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.JCReportsList, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(DZServiceActivity.this, "uid"));
        // 添加到请求队列
        CallServer.getRequestInstance().add(DZServiceActivity.this, 0, mRequest,
                new CustomHttpListener(DZServiceActivity.this, true, JianCeList.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        model = ((JianCeList) data);
                        if (page == 1) {
                            Temp_listfw.clear();
                        }
                        Temp_listfw.addAll(model.getFooter());
                        if (Temp_listfw.size() == 0) {
                            li_null.setVisibility(View.VISIBLE);
                            rlistService.setVisibility(View.GONE);
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        mRefresh.setRefreshing(false);
                    }
                }, true, true);
    }


    public class JianceListAdapter extends BaseLoadMoreHeaderAdapter<JianCeList.FooterBean> {


        public JianceListAdapter(Context mContext, RecyclerView recyclerView, List<JianCeList.FooterBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, RecyclerView.ViewHolder holder, JianCeList.FooterBean s) {
            TextView tv_dzservice_time = (TextView) holder.itemView.findViewById(R.id.tv_dzservice_time);
            TextView tv_dzservice_content = (TextView) holder.itemView.findViewById(R.id.tv_dzservice_content);
            View view_dzservice_ban = (View) holder.itemView.findViewById(R.id.view_dzservice_ban);
            LinearLayout li_dzservice_man = (LinearLayout) holder.itemView.findViewById(R.id.li_dzservice_man);
            //新加--精确报告+开始锻炼
            tv_dzservice_time.setText(s.getCreateDate());
            tv_dzservice_content.setText(s.getPingjia());
            if (holder.getLayoutPosition() == Temp_listfw.size() - 1) {
                li_dzservice_man.setVisibility(View.GONE);
            } else {
                li_dzservice_man.setVisibility(View.VISIBLE);
            }
        }
    }
}


